Make HttpStatusAgent provide redirect info (#1590)

* Make HttpStatusAgent provide redirect info

* Mock to_hash for HttpStatusAgent specs

* Refactor HttpStatusAgent spec mock responses

Alex Jordan 8 years ago
parent
commit
c1095330ea
2 changed files with 21 additions and 6 deletions
  1. 2 1
      app/models/agents/http_status_agent.rb
  2. 19 5
      spec/controllers/http_status_agent_spec.rb

+ 2 - 1
app/models/agents/http_status_agent.rb

@@ -82,7 +82,8 @@ module Agents
82 82
 
83 83
       # Deal with failures
84 84
       if measured_result.result
85
-        payload.merge!({ 'response_received' => true, 'status' => current_status })
85
+        final_url = boolify(interpolated['disable_redirect_follow']) ? url : measured_result.result.to_hash[:url]
86
+        payload.merge!({ 'final_url' => final_url, 'redirected' => (url != final_url), 'response_received' => true, 'status' => current_status })
86 87
         # Deal with headers
87 88
         if local_headers.present?
88 89
           header_results = measured_result.result.headers.select {|header, value| local_headers.include?(header)}

+ 19 - 5
spec/controllers/http_status_agent_spec.rb

@@ -1,5 +1,9 @@
1 1
 require 'rails_helper'
2 2
 
3
+class MockResponse < Struct.new(:status, :headers, :url)
4
+  alias_method :to_hash, :to_h
5
+end
6
+
3 7
 describe 'HttpStatusAgent' do
4 8
 
5 9
   let(:agent) do
@@ -109,7 +113,7 @@ describe 'HttpStatusAgent' do
109 113
       let(:header_value) { SecureRandom.uuid }
110 114
 
111 115
       let(:event_with_a_successful_ping) do
112
-        agent.faraday.set(successful_url, Struct.new(:status, :headers).new(status_code, {}))
116
+        agent.faraday.set(successful_url, MockResponse.new(status_code, {}, successful_url))
113 117
         Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: "" } }
114 118
       end
115 119
 
@@ -208,10 +212,20 @@ describe 'HttpStatusAgent' do
208 212
         expect(agent.the_created_events[0][:payload]['url']).to eq(successful_url)
209 213
       end
210 214
 
215
+      it "should return the final url" do
216
+        agent.receive events
217
+        expect(agent.the_created_events[0][:payload]['final_url']).to eq(successful_url)
218
+      end
219
+
220
+      it "should return whether the url redirected" do
221
+        agent.receive events
222
+        expect(agent.the_created_events[0][:payload]['redirected']).to eq(false)
223
+      end
224
+
211 225
       describe "but the ping returns a status code of 0" do
212 226
 
213 227
         let(:event_with_a_successful_ping) do
214
-          agent.faraday.set(successful_url, Struct.new(:status, :headers).new(0, {}))
228
+          agent.faraday.set(successful_url, MockResponse.new(0, {}, successful_url))
215 229
           Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: "" } }
216 230
         end
217 231
 
@@ -241,7 +255,7 @@ describe 'HttpStatusAgent' do
241 255
       describe "but the ping returns a status code of -1" do
242 256
 
243 257
         let(:event_with_a_successful_ping) do
244
-          agent.faraday.set(successful_url, Struct.new(:status, :headers).new(-1, {}))
258
+          agent.faraday.set(successful_url, MockResponse.new(-1, {}, successful_url))
245 259
           Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: "" } }
246 260
         end
247 261
 
@@ -302,7 +316,7 @@ describe 'HttpStatusAgent' do
302 316
 
303 317
       describe "with a header specified" do
304 318
         let(:event_with_a_successful_ping) do
305
-          agent.faraday.set(successful_url, Struct.new(:status, :headers).new(status_code, {header => header_value}))
319
+          agent.faraday.set(successful_url, MockResponse.new(status_code, {header => header_value}, successful_url))
306 320
           Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: header } }
307 321
         end
308 322
 
@@ -318,7 +332,7 @@ describe 'HttpStatusAgent' do
318 332
         let(:nonexistant_header) { SecureRandom.uuid }
319 333
 
320 334
         let(:event_with_a_successful_ping) do
321
-          agent.faraday.set(successful_url, Struct.new(:status, :headers).new(status_code, {header => header_value}))
335
+          agent.faraday.set(successful_url, MockResponse.new(status_code, {header => header_value}, successful_url))
322 336
           Event.new.tap { |e| e.payload = { url: successful_url, headers_to_save: header + "," + nonexistant_header } }
323 337
         end
324 338